﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Data;
using System.Xml;
using System.Xml.XPath;

namespace XMM
{
    public class Settings : Object
    {
        /// <summary>
        /// Database-ID of the last logged-in audition.
        /// </summary>
        public int LastAuditionID = 0;

        /// <summary>
        /// Time of the last successfull backup.
        /// </summary>
        public DateTime LastBackup = DateTime.MinValue;

        /// <summary>
        /// Path to library-content-files.
        /// </summary>
        /// <remarks>e.g. covers, thumbnails, etc.</remarks>
        public string LibraryDataStorage = String.Empty;

        public string VLCExecutablePath = String.Empty;

        public int VideoPlayerExtensions = 0;

        public string DatabaseStoragePath = String.Empty;

        public bool CheckForUpdates = false;

        public int CheckForUpdatesInterval = 0;

        public DateTime LastUpdate = DateTime.MinValue;

        /// <summary>
        /// Initialize a new Settings-Object and load values from settings.xml.
        /// </summary>
        public Settings()
        {
            Load();
        }

        public bool Save()
        {
            try
            {
                XmlDocument xdSettings = new XmlDocument();
                xdSettings.Load(Tools.getInstallationPath() + "settings.xml");

                XmlNode xnTopNode = xdSettings.SelectSingleNode("//settings");
                XmlNode xnGUISettings = xdSettings.SelectSingleNode("//gui");
                XmlNode xnDatabaseSettings = xdSettings.SelectSingleNode("//database");
                XmlNode xnUserSettings = xdSettings.SelectSingleNode("//user");
                XmlNode xnOnlineSettings = xdSettings.SelectSingleNode("//online");
                if (xnOnlineSettings == null)
                {
                    xnOnlineSettings = xdSettings.CreateElement("online");
                    xnTopNode.AppendChild(xnOnlineSettings);
                }

                XmlNode xnLastAuditionID = xnUserSettings.SelectSingleNode("//LastAudition");
                xnLastAuditionID.InnerText = LastAuditionID.ToString();

                XmlNode xnLastBackup = xnUserSettings.SelectSingleNode("//LastBackup");
                xnLastBackup.InnerText = LastBackup.ToShortDateString();

                XmlNode xnLibraryDataStorage = xnUserSettings.SelectSingleNode("//LibraryDataStorage");
                xnLibraryDataStorage.InnerText = LibraryDataStorage;

                XmlNode xnVLCExecutablePath = xnGUISettings.SelectSingleNode("//VLCExecutablePath");
                xnVLCExecutablePath.InnerText = VLCExecutablePath;

                XmlNode xnVideoPlayerExtensions = xnGUISettings.SelectSingleNode("//VideoPlayerExtensions");
                xnVideoPlayerExtensions.InnerText = VideoPlayerExtensions.ToString();

                XmlNode xnDatabaseStoragePath = xnDatabaseSettings.SelectSingleNode("//DatabaseStoragePath");
                xnDatabaseStoragePath.InnerText = DatabaseStoragePath;

                XmlNode xnCheckForUpdates = xnOnlineSettings.SelectSingleNode("//CheckForUpdates");
                if (xnCheckForUpdates == null)
                {
                    xnCheckForUpdates = xdSettings.CreateElement("CheckForUpdates");
                    xnOnlineSettings.AppendChild(xnCheckForUpdates);
                }
                xnCheckForUpdates.InnerText = CheckForUpdates.ToString();

                XmlNode xnCheckForUpdatesInterval = xnOnlineSettings.SelectSingleNode("//CheckForUpdatesInterval");
                if (xnCheckForUpdatesInterval == null)
                {
                    xnCheckForUpdatesInterval = xdSettings.CreateElement("CheckForUpdatesInterval");
                    xnOnlineSettings.AppendChild(xnCheckForUpdatesInterval);
                }
                xnCheckForUpdatesInterval.InnerText = CheckForUpdatesInterval.ToString();

                XmlNode xnLastUpdate = xnOnlineSettings.SelectSingleNode("//LastUpdate");
                if (xnLastUpdate == null)
                {
                    xnLastUpdate = xdSettings.CreateElement("LastUpdate");
                    xnOnlineSettings.AppendChild(xnLastUpdate);
                }
                if(DateTime.Parse(xnLastUpdate.InnerText) < LastUpdate)
                    xnLastUpdate.InnerText = LastUpdate.ToString();

                xdSettings.Save(Tools.getInstallationPath() + "settings.xml");

                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fehler beim Speichern der Einstellungen in \"" + Tools.getInstallationPath() + "settings.xml\": " + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            return false;
        }

        /// <summary>
        /// (Re-)Load settings from settings.xml.
        /// </summary>
        /// <returns>[true] if settings where loaded successfully.</returns>
        public bool Load()
        {
            try
            {
                XmlDocument xdSettings = new XmlDocument();
                xdSettings.Load(Tools.getInstallationPath() + "settings.xml");

                //XmlNode xnTopNode = xdSettings.SelectSingleNode("//settings");
                XmlNode xnGUISettings = xdSettings.SelectSingleNode("//gui");
                XmlNode xnDatabaseSettings = xdSettings.SelectSingleNode("//database");
                XmlNode xnUserSettings = xdSettings.SelectSingleNode("//user");
                XmlNode xnOnlineSettings = xdSettings.SelectSingleNode("//online");
                if (xnOnlineSettings == null)
                {
                    xnOnlineSettings = xdSettings.CreateElement("online");
                    //xnTopNode.AppendChild(xnOnlineSettings);
                }

                XmlNode xnLastAuditionID = xnUserSettings.SelectSingleNode("//LastAudition");
                if(xnLastAuditionID.InnerText != String.Empty)
                    LastAuditionID = int.Parse(xnLastAuditionID.InnerText);

                XmlNode xnLastBackup = xnUserSettings.SelectSingleNode("//LastBackup");
                if (xnLastBackup.InnerText != String.Empty)
                    LastBackup = DateTime.Parse(xnLastBackup.InnerText);

                XmlNode xnLibraryDataStorage = xnUserSettings.SelectSingleNode("//LibraryDataStorage");
                if (xnLibraryDataStorage.InnerText != String.Empty)
                    LibraryDataStorage = xnLibraryDataStorage.InnerText;

                XmlNode xnVLCExecutablePath = xnGUISettings.SelectSingleNode("//VLCExecutablePath");
                if (xnVLCExecutablePath.InnerText != String.Empty)
                    VLCExecutablePath = xnVLCExecutablePath.InnerText;

                XmlNode xnVideoPlayerExtensions = xnGUISettings.SelectSingleNode("//VideoPlayerExtensions");
                if (xnVideoPlayerExtensions.InnerText != String.Empty)
                    VideoPlayerExtensions = int.Parse(xnVideoPlayerExtensions.InnerText);

                XmlNode xnDatabaseStoragePath = xnDatabaseSettings.SelectSingleNode("//DatabaseStoragePath");
                if (xnDatabaseStoragePath.InnerText != String.Empty)
                    DatabaseStoragePath = xnDatabaseStoragePath.InnerText;

                XmlNode xnCheckForUpdates = xnOnlineSettings.SelectSingleNode("//CheckForUpdates");
                if (xnCheckForUpdates == null)
                {
                    xnCheckForUpdates = xdSettings.CreateElement("CheckForUpdates");
                    xnCheckForUpdates.InnerText = "false";
                }
                CheckForUpdates = bool.Parse(xnCheckForUpdates.InnerText);

                XmlNode xnCheckForUpdatesInterval = xnOnlineSettings.SelectSingleNode("//CheckForUpdatesInterval");
                if (xnCheckForUpdatesInterval == null)
                {
                    xnCheckForUpdatesInterval = xdSettings.CreateElement("CheckForUpdatesInterval");
                    xnCheckForUpdatesInterval.InnerText = "7";
                }
                CheckForUpdatesInterval = int.Parse(xnCheckForUpdatesInterval.InnerText);

                XmlNode xnLastUpdate = xnOnlineSettings.SelectSingleNode("//LastUpdate");
                if (xnLastUpdate == null)
                {
                    xnLastUpdate = xdSettings.CreateElement("LastUpdate");
                    xnLastUpdate.InnerText = DateTime.MinValue.ToString();
                }
                LastUpdate = DateTime.Parse(xnLastUpdate.InnerText);

                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fehler beim Laden der Einstellungen: " + ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
            }            

            return false;
        }
    }
}
